﻿/*
 * Copyright 2010 Edward L. Stauff.
 */

using System;
using System.Collections.Generic;
using System.Text;
using Stauffware.Common.Core;
using NUnit.Framework;

namespace UnitTests.Core
{
	/*
	 *	custom exception class
	 *	exception data
	 *	lambda expression
	 *	system function
	 */
	public class ErrorUtilsTests
	{
		[Test()]
		public static void Test1 ()
		{
			MyInnerClass mic = new MyInnerClass();

			try
			{
				mic.DoSomething();
			}
			catch (Exception x)
			{
				StringBuilder sb = new StringBuilder();
				ErrorUtils.DumpExceptionChain(x, sb);
				System.Console.WriteLine(sb);
			}
		}

		class MyInnerClass
		{
			public void DoSomething ()
			{
				List<string> listToSort = new List<string>() { "Hello, world!", "Goodbye, world!" };

				listToSort.Sort(MyStringComparer);
			}

			private int MyStringComparer (string a, string b)
			{
				InexplicableOccurrance uhOh = new InexplicableOccurrance("Look ma, no hands!");
				uhOh.Data.Add("where", "driveway");
				uhOh.Data.Add("numStitches", 17);
				uhOh.Data.Add(23, null);
				throw uhOh;
			}
		}

		[TestCase(null, "NULL")]
		[TestCase('k', "'k' (0x6B)")]
		[TestCase((byte) 0, "0")]
		[TestCase((sbyte) -123, "-123 (0x85)")]
		[TestCase("", "\"\"")]
		[TestCase("return\r newline\n tab\t doublequote\" backslash\\",
					"\"return\\r newline\\n tab\\t doublequote\\\" backslash\\\\\"")]
		[TestCase(Enumeration1.Foo, "Foo")]
		[TestCase(Enumeration2.Nothing, "Nothing")]
		[TestCase(Enumeration2.Foo, "Foo")]
		[TestCase(Enumeration2.Bar, "Bar")]
		[TestCase(Enumeration2.Foobar, "Foobar")]
		[TestCase(Enumeration2.Quux, "Quux")]
		[TestCase(Enumeration2.Quux | Enumeration2.Foo, "Foo, Quux")]
		public static void ValueToString (object value, string expected)
		{
			string actual = ErrorUtils.ValueToString(value);
			NUnit.Framework.Assert.AreEqual(expected, actual);
		}

		enum Enumeration1
		{
			Foo = 1,
			Bar = 2,
			Foobar = 3
		}

		[Flags]
		enum Enumeration2
		{
			Nothing = 0,
			Foo = 1,
			Bar = 2,
			Foobar = 3,
			Quux = 4
		}
	}

	class InexplicableOccurrance : Exception
	{
		public InexplicableOccurrance (string msg)
			: base(msg)
		{
		}
	}

}
